package com.lib.common.helper.sharehelper

import com.lib.common.helper.sharehelper.entity.ShareData
import com.lib.common.helper.sharehelper.entity.ShareType
import com.lib.common.helper.sharehelper.share.*
import com.lib.framework.base.BaseActivity
import com.lib.framework.manager.ContextManager
import com.lib.framework.manager.thread.mainThreadRun

/**
 * 分享帮助类
 *
 * @author keanbin
 */
object ShareHelper {

    private var mContext = ContextManager.applicationContext

    /**
     * 分享对象列表
     */
    private var mShareMap = mutableMapOf<ShareType, IShare>()

    /**
     * 是否已经初始化
     */
    private var mIsInit = false

    /**
     * 初始化
     */
    fun init() {
        if (mIsInit) return
        mIsInit = true

        addShare(QQShare(mContext))
        addShare(QQZoneShare(mContext))
//        addShare(WeiBoShare(mContext))
        addShare(WeiXinFriendShare(mContext))
        addShare(WeiXinFriendCircleShare(mContext))

        for ((_, share) in mShareMap) {
            share.init()
        }
    }

    /**
     * 分享，请在线程中调用该函数
     */
    fun share(activity: BaseActivity,  shareType: ShareType, shareData: ShareData, l: AShareListener) {
        l.mShareType = shareType

        val share = mShareMap[shareType]
        if (share == null) {
            mainThreadRun {
                l.onShareFail("暂不支持该类型分享")
            }
            return
        }

        val ret =  share.share(activity, shareData, l)
        if (!ret) {
            mainThreadRun {
                l.onShareFail("分享失败")
            }
        }
    }

    private fun addShare(share: IShare) {
        mShareMap[share.getShareType()] = share
    }
}